﻿'功能說明：系統主網址下的主要內容區，分menu,Split及MainPage1,2011/3/8日修改,原從資源檔取得其他語言別的面頁名稱,為增強彈性修改從DB取得,但只限繁中.簡體及英文3種語文.
'建立人員: andy
'建立日期：2011/1/31
'****************************************************
Imports System.Threading
Imports System.Globalization
Imports System.Resources
Partial Class WebForm_Main_Menu
    Inherits BasePage

    Dim intLevel As Integer = 0
    


    Protected Sub BuildTreeView()
        Dim db As New SqlDataSource
        Dim strRootName As String = ""
        Dim Nodes As Web.UI.WebControls.TreeNodeCollection


        db.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings(dbName).ToString
        db.SelectCommand = "select distinct A.ProgramID,A.URL ,A.RootName ,A.Name  ,A.ChnName,A.EngName ,A.OrderByID  from Menu A   left join MenuRights B on A.ProgramID=B.ProgramID   left join GroupRights C on C.ProgramID=B.ProgramID and  C.SeqNo=B.SeqNo "
        db.SelectCommand += " left join GroupMember D on C.GroupID=D.GroupID  where ( (A.NeedControl=0)   or   ( D.UID='" & UID & "'  and C.UseRight=1) ) and A.RootName='' and A.IsMenu=1 order by OrderByID "
        Dim dv As Data.DataView = db.Select(New DataSourceSelectArguments)

        For i As Integer = 0 To dv.Count - 1
            If (strRootName <> dv.Item(i).Item("RootName").ToString) Then

            End If
            Nodes = Me.TreeView1.Nodes
            'AddNewNode(dv.Item(i).Item("ProgramID").ToString, dv.Item(i).Item("RootName").ToString, "", 0)
            AddNewNode(dv.Item(i).Item("ProgramID").ToString, dv.Item(i).Item("Name").ToString, dv.Item(i).Item("URL").ToString, 0, dv.Item(i).Item("ChnName").ToString, dv.Item(i).Item("EngName").ToString)
            BuildSubTreeView(dv.Item(i).Item("Name").ToString)
            strRootName = dv.Item(i).Item("RootName").ToString()
            intLevel = intLevel + 1
        Next
        dv = Nothing
        db = Nothing
    End Sub


    Protected Sub BuildSubTreeView(byval RootName As string )
        Dim db As New SqlDataSource
        Dim strRootName As String = ""
        Dim Nodes As Web.UI.WebControls.TreeNodeCollection
        ' Dim intLevel As Integer = 0

        db.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings(dbName).ToString
        db.SelectCommand = "select distinct A.ProgramID,A.URL ,A.RootName ,A.Name ,A.ChnName,A.EngName  from Menu A   left join MenuRights B on A.ProgramID=B.ProgramID   left join GroupRights C on C.ProgramID=B.ProgramID and  C.SeqNo=B.SeqNo "
        db.SelectCommand += " left join GroupMember D on C.GroupID=D.GroupID  where ( (A.NeedControl=0)   or   ( D.UID='" & UID & "'  and C.UseRight=1) ) and A.IsMenu =1 and A.Name<>'" & RootName & "' and  A.RootName='" & RootName & "'"
        Dim dv As Data.DataView = db.Select(New DataSourceSelectArguments)
        Nodes = Me.TreeView1.Nodes
        For i As Integer = 0 To dv.Count - 1

            'AddNewNode(dv.Item(i).Item("ProgramID").ToString, dv.Item(i).Item("RootName").ToString, "", 0)
            strRootName = dv.Item(i).Item("RootName").ToString()
            If (strRootName <> dv.Item(i).Item("RootName").ToString) Then

                '
            End If

            AddNewNode(dv.Item(i).Item("ProgramID").ToString, dv.Item(i).Item("Name").ToString, dv.Item(i).Item("URL").ToString, 2, dv.Item(i).Item("ChnName").ToString, dv.Item(i).Item("EngName").ToString)
        Next
        dv = Nothing
        db = Nothing
    End Sub
    Private Sub AddNewNode(ByVal strMID As String, ByVal strName As String, ByVal strURL As String, ByVal intLevel11 As Integer, ByVal ChnName As String, ByVal EngName As String)
        Dim NewNode As TreeNode

        NewNode = New TreeNode
        Try
            Select (GetUserLanguage().ToLower)
                Case "en-us"
                    NewNode.Text = EngName
                Case "zh-cn"
                    NewNode.Text = ChnName
                Case Else
                    NewNode.Text = strName
            End Select


            If strName <> "" And NewNode.Text = "" Then
                NewNode.Text = strName
            End If
        Catch ex As Exception
            NewNode.Text = strName
        End Try

        'NewNode.Text = strName
        NewNode.Value = strMID
        If intLevel11 = 0 Then
            If strMID = "1" Then
                NewNode.SelectAction = TreeNodeSelectAction.SelectExpand
            Else
                NewNode.SelectAction = TreeNodeSelectAction.SelectExpand
            End If
            If strURL <> "" Then
                NewNode.NavigateUrl = "javascript:RedirectPage('" & Me.ResolveUrl(strURL) & "');"
            Else
                NewNode.NavigateUrl = ""
            End If

            ' NewNode.Target = "fraMain"

            Me.TreeView1.Nodes.Add(NewNode)
        Else
            If strURL <> "" Then
                NewNode.NavigateUrl = "javascript:RedirectPage('" & Me.ResolveUrl(strURL) & "');"
            Else
                NewNode.NavigateUrl = ""
            End If
            ' NewNode.Target = "fraMain"
            NewNode.SelectAction = TreeNodeSelectAction.SelectExpand
            Me.TreeView1.Nodes.Item(intLevel).ChildNodes.Add(NewNode)
        End If
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim stJava As String
        If Not IsPostBack Then
            If Not IsNothing(Session("myURL")) Then

                stJava = "<script language=JavaScript>  RedirectPage('" & Session("myURL") & "');</script>"
                Page.ClientScript.RegisterClientScriptBlock(GetType(WebForm_Main_Menu), "returnUrl", stJava)
                Session.Remove("myURL")
            End If
        End If
        If Page.IsPostBack = False Then

            BuildTreeView()
            TreeView1.CollapseAll() '縮起來
        Else

        End If
    End Sub




    'Protected Sub TreeView1_SelectedNodeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TreeView1.SelectedNodeChanged
    '    Page.ClientScript.RegisterClientScriptBlock(GetType(BasePage), "CloseFun", "<script language=JavaScript>  funClose();  </script>")
    'End Sub

    'Protected Sub TreeView1_TreeNodePopulate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.TreeNodeEventArgs) Handles TreeView1.TreeNodePopulate
    '    Page.ClientScript.RegisterClientScriptBlock(GetType(BasePage), "CloseFun", "<script language=JavaScript>  funClose();  </script>")
    'End Sub

    'Protected Sub TreeView1_TreeNodeCheckChanged(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.TreeNodeEventArgs) Handles TreeView1.TreeNodeCheckChanged
    '    Page.ClientScript.RegisterClientScriptBlock(GetType(BasePage), "CloseFun", "<script language=JavaScript>  funClose();  </script>")
    'End Sub

    'Protected Sub TreeView1_SelectedNodeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TreeView1.SelectedNodeChanged
    '    Page.ClientScript.RegisterClientScriptBlock(GetType(BasePage), "CloseFun", "<script language=JavaScript>  funClose();  </script>")
    'End Sub

    'Protected Sub TreeView1_TreeNodeCheckChanged(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.TreeNodeEventArgs) Handles TreeView1.TreeNodeCheckChanged
    '    Page.ClientScript.RegisterClientScriptBlock(GetType(BasePage), "CloseFun", "<script language=JavaScript>  funClose();  </script>")
    'End Sub

    'Protected Sub TreeView1_TreeNodePopulate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.TreeNodeEventArgs) Handles TreeView1.TreeNodePopulate
    '    Page.ClientScript.RegisterClientScriptBlock(GetType(BasePage), "CloseFun", "<script language=JavaScript>  funClose();  </script>")
    'End Sub
End Class
